using System;
using System.IO;
using System.Linq.Expressions;
using System.Web.Mvc;

namespace FwkLight.Web.MVC
{
  public static class HtmlHelperExtensions
  {
    /// <summary>
    /// Determines if the current view equals the specified action
    /// </summary>
    /// <typeparam name="TController">The type of the controller.</typeparam>
    /// <param name="helper">Url Helper</param>
    /// <param name="action">The action to check.</param>
    /// <returns>
    ///     <c>true</c> if the specified action is the current view; otherwise, <c>false</c>.
    /// </returns>
    public static bool IsAction<TController> ( this HtmlHelper helper, LambdaExpression action ) where TController : Controller
    {
      MethodCallExpression call = action.Body as MethodCallExpression;
      if (call == null)
      {
        throw new ArgumentException("Expression must be a method call", "action");
      }

      string fileNameWithoutExtension = Path.GetFileNameWithoutExtension(((WebFormView)helper.ViewContext.View).ViewPath);
      return (call.Method.Name.Equals(fileNameWithoutExtension, StringComparison.OrdinalIgnoreCase) &&
              typeof(TController) == helper.ViewContext.Controller.GetType());
    }

    ///// <summary>
    ///// Determines if the current view equals the specified action
    ///// </summary>
    ///// <param name="helper">Url Helper</param>
    ///// <param name="actionName">Name of the action.</param>
    ///// <returns>
    /////     <c>true</c> if the specified action is the current view; otherwise, <c>false</c>.
    ///// </returns>
    //public static bool IsAction ( this HtmlHelper helper, string actionName )
    //{
    //  if (String.IsNullOrEmpty(actionName))
    //  {
    //    throw new ArgumentException("Please specify the name of the action", "actionName");
    //  }
    //  string controllerName = helper.ViewContext.RouteData.GetRequiredString("controller");
    //  return IsAction(helper, actionName, controllerName);
    //}

    /// <summary>
    /// Determines if the current view equals the specified action
    /// </summary>
    /// <param name="helper">Url Helper</param>
    /// <param name="actionName">Name of the action.</param>
    /// <param name="controllerName">Name of the controller.</param>
    /// <returns>
    ///     <c>true</c> if the specified action is the current view; otherwise, <c>false</c>.
    /// </returns>
    public static bool IsAction ( this HtmlHelper helper, string actionName, string controllerName )
    {
      if (String.IsNullOrEmpty(actionName))
      {
        throw new ArgumentException("Please specify the name of the action", "actionName");
      }
      if (String.IsNullOrEmpty(controllerName))
      {
        throw new ArgumentException("Please specify the name of the controller", "controllerName");
      }

      if (!controllerName.EndsWith("Controller", StringComparison.OrdinalIgnoreCase))
      {
        controllerName = controllerName + "Controller";
      }

      string fileNameWithoutExtension = Path.GetFileNameWithoutExtension(((WebFormView)helper.ViewContext.View).ViewPath);
      bool isOnView = actionName.Equals(fileNameWithoutExtension, StringComparison.OrdinalIgnoreCase);
      return isOnView && helper.ViewContext.Controller.GetType().Name.Equals(controllerName, StringComparison.OrdinalIgnoreCase);
    }

    /// <summary>
    /// Determines if the current request is on the specified controller
    /// </summary>
    /// <param name="helper">The helper.</param>
    /// <param name="controllerName">Name of the controller.</param>
    /// <returns>
    ///     <c>true</c> if the current view is on the specified controller; otherwise, <c>false</c>.
    /// </returns>
    public static bool IsController ( this HtmlHelper helper, string controllerName )
    {
      if (String.IsNullOrEmpty(controllerName))
      {
        throw new ArgumentException("Please specify the name of the controller", "controllerName");
      }

      if (!controllerName.EndsWith("Controller", StringComparison.OrdinalIgnoreCase))
      {
        controllerName = controllerName + "Controller";
      }

      return helper.ViewContext.Controller.GetType().Name.Equals(controllerName, StringComparison.OrdinalIgnoreCase);
    }

    public static string ControllerName( this HtmlHelper helper)
    {
      return helper.ViewContext.Controller.GetType().Name.ToLower().Replace("controller", "");
    }



    ///// <summary>
    ///// Determines if the current request is on the specified controller
    ///// </summary>
    ///// <typeparam name="TController">The type of the controller.</typeparam>
    ///// <param name="helper">The helper.</param>
    ///// <returns>
    /////     <c>true</c> if the current view is on the specified controller; otherwise, <c>false</c>.
    ///// </returns>
    //public static bool IsController<TController> ( this HtmlHelper helper ) where TController : Controller
    //{
    //  return (typeof(TController) == helper.ViewContext.Controller.GetType());
    //}
  }
}